package com.ruoyi.hotel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.hotel.domain.bo.HmCheckinBo;
import com.ruoyi.hotel.domain.vo.HmCheckinVo;
import com.ruoyi.hotel.domain.HmCheckin;
import com.ruoyi.hotel.mapper.HmCheckinMapper;
import com.ruoyi.hotel.service.IHmCheckinService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-01
 */
@RequiredArgsConstructor
@Service
public class HmCheckinServiceImpl implements IHmCheckinService {

    private final HmCheckinMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public HmCheckinVo queryById(Long checkinId){
        return baseMapper.selectVoById(checkinId);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<HmCheckinVo> queryPageList(HmCheckinBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HmCheckin> lqw = buildQueryWrapper(bo);
        Page<HmCheckinVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<HmCheckinVo> queryList(HmCheckinBo bo) {
        LambdaQueryWrapper<HmCheckin> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HmCheckin> buildQueryWrapper(HmCheckinBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HmCheckin> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getBookId() != null, HmCheckin::getBookId, bo.getBookId());
        lqw.eq(bo.getCheckinWorkerId() != null, HmCheckin::getCheckinWorkerId, bo.getCheckinWorkerId());
        lqw.eq(bo.getCheckinTime() != null, HmCheckin::getCheckinTime, bo.getCheckinTime());
        lqw.eq(bo.getCheckoutTime() != null, HmCheckin::getCheckoutTime, bo.getCheckoutTime());
        lqw.eq(bo.getIsDone() != null, HmCheckin::getIsDone, bo.getIsDone());
        lqw.eq(bo.getCustomerId() != null, HmCheckin::getCustomerId, bo.getCustomerId());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(HmCheckinBo bo) {
        HmCheckin add = BeanUtil.toBean(bo, HmCheckin.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCheckinId(add.getCheckinId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(HmCheckinBo bo) {
        HmCheckin update = BeanUtil.toBean(bo, HmCheckin.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HmCheckin entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    @Override
    public Boolean checkBook(Long id){
        QueryWrapper<HmCheckin> qw=new QueryWrapper<>();
        qw.eq("customer_id",id).eq("is_done",false);
        return baseMapper.exists(qw);
    }

    @Override
    public HmCheckin getCheckinNow(Long id){

        QueryWrapper<HmCheckin> qw=new QueryWrapper<>();
        qw.eq("customer_id",id).eq("is_done",false);

        if(!baseMapper.exists(qw)){
            return null;
        }
        HmCheckin hmCheckin=baseMapper.selectOne(qw);

        return hmCheckin;
    }

    @Override
    public List<HmCheckin> getValidCheckin(){
        QueryWrapper<HmCheckin> qw=new QueryWrapper<>();
        qw.eq("is_done",false);

        List<HmCheckin> hmCheckin=baseMapper.selectList(qw);

        return hmCheckin;
    }


}
